/*
 * Matthew Cramer, Jesse Martinez, Marc Mendoza 
 * CSE 325 Lab 6
 * File: TestingInterface.c
 * Description: Manager that handles calls to the Queue Manager, File System
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "../QueueManager/QueueManager.h"
#include "../FileSystem/FileSystem.h"
//#include "../BadBlock/BadBlock.h"
#define MAX_FILES 60
#define MAX_EXTRA_BLOCKS 256 
#define MAX_BUF 10
#define BLOCK_SIZE 4
#define MAX_FILE_SIZE 1024
#define MAX_HD_SIZE 65536

void list(int,int);
char command[500],device[0][50],filesystem[1][50],directories[100][50],files[60][50];
int dirCount=0, fileCount=0;
char argument[3][30];

int  main(int argc, char *argv[])
{
	char *temp;
	int exit = 1, check=0, manualInput = 1, initiate=0;
	int i=0;//total managers
	FILE *file;
	
    //Open File for Use
	if(argc == 2)
	{
		file = fopen(argv[1], "r");
		manualInput =0;
	}
	
	while(exit != 0)
	{
        printf(": ");
        
        //For terminal use
		if(manualInput == 1)
		{
			scanf("%[^\n]",command);
			getchar();
		}
		
        //For input file use
		else if(manualInput == 0)
		{
            
            if (fgets(command, sizeof command, file)) 
            {
                char* p = strchr(command,'\n');
                if (p)
                    *p = '\0';
                printf("%s\n", command);
            }
		}
		
		//uses a string tokenizer to split files based on whitespace
		temp = strtok(command, " ");
		
		strcpy(command, temp);
		
        //Command to display help for list of commands and use
        if(strcmp(command, "help") == 0)
        {
            //Display Menu
            
            printf("\nFile Systems Commands: \n");
            printf("init_fs device\n");
            printf("list {DEVICES | FILEINFO filename | DIRECTORY directory_name\n");
            printf("format device fs_name blocksize(4KB)\n");
            printf("mount fs_name\n");
            printf("create_directory directory_name\n");
            printf("open directory_name filename [new | read-only | read-write\n");
            printf("read directory_name filehandle block_number buf_ptr\n");
            printf("write directory_name filehandle block_number buf_ptr\n");
            printf("close directory_name filehandle\n");
            printf("delete directory_name {filename}\n");
            printf("bad_block block_number\n");
            printf("unmount fs_name\n");
            printf("help\n");
            printf("EXIT\n\n");
            
        }
        //If command is init device
		else if(strcmp(command, "init_fs") == 0)
		{
            if(initiate==2)
                printf("ERROR: FILE SYSTEM ON DEVICE IS MOUNTED.  PLEASE UNMOUNT FIRST\n");
            //Erase all data on file system and start from scratch.
            else
            {
                int k=0;
                temp = strtok(NULL, " ");
                //puts all managers into array
                
                //Check to see if correct number of arguments
                while(temp != NULL)
                {
                    strcpy(device[k], temp);
                    temp = strtok(NULL, " ");
                    k++;
                }
                
                //If correct number of arguments, allocate hard disk linked list
                if(k == 1)
                {
                    //Initialize the hard disk device header
                    initHD();
                    
                    printf("DEVICE KNOWN: %s\n",device[0]);
                    filesystem[0][0] = '\0';
                    initiate=1;
                }
                else
                {
                    printf("ERROR: INCORRECT ARGUMENTS\n");
                }
            }
		}
        //If Command is List
		else if(strcmp(command, "list") == 0 && initiate>0)
		{
			temp = strtok(NULL, " ");
            int k=0;
            //Check to see if arguments are correct
            while(temp != NULL)
			{
				strcpy(argument[k], temp);
				temp = strtok(NULL, " ");
				k++;
			}
            //If parameter requested
			if(k > 0)
			{
                //List device and file system known to system
				if(strcmp(argument[0], "DEVICES") == 0 && k == 1)
                {
                    list(-2,-1);
                }
                
                else if (strcmp(argument[0], "FILEINFO") == 0 && k == 2)
                {
                    //List file information for specified file
                    if(k == 2)
                    {
                        int i=0,temp1=0;
                        while(i<60 && temp1==0)
                        {
                            if(strcmp(argument[1],files[i]) == 0)
                                temp1=1;
                            else
                                i++;
                        }
                        if(temp1==1)
                            list(-3,i);
                        else
                            printf("ERROR: FILE NAME NOT FOUND\n");
                    }
                    //list file information for all files
                    else if(k == 1)
                        list(-3,-1);
                }
                
                else if (strcmp(argument[0], "DIRECTORY") == 0 && k == 2)
                {
                    //List directories and file in them for specified directory
                    if(k==2)
                    {
                        int i=0,temp1=0;
                        while(i<100 && temp1==0)
                        {
                            if(strcmp(argument[1],directories[i]) == 0)
                                temp1=1;
                            else
                                i++;
                        }
                        if(temp1==1)
                            list(-4,i);
                        else
                            printf("ERROR: DIRECTORY NAME NOT FOUND\n");
                    }
                    //List all directories and files under directories
                    else if (k==1)
                        list(-4,-1);
                }
                else
                {
                    printf("ERROR: INCORRECT ARGUMENT(S)\n");
                }
			}
            //List all managers being used
			else if(k==0)
			{
                list(-1,-1);
			}
            else
                printf("ERROR: INCORRECT ARGUMENTS\n");  
		}
        //If command is format
		else if(strcmp(command, "format") == 0 && initiate>0)
		{
            if(initiate>1)
            {
                printf("ERROR: FILE SYSTEM HAS BEEN FORMATTED ALREADY. TO CLEAR DATA, CALL init_fs\n");
			}
            else
            {
                temp = strtok(NULL, " ");
                int k=0;
                //Check to see if correct number of arguments
                while(temp != NULL)
                {
                    strcpy(argument[k], temp);
                    temp = strtok(NULL, " ");
                    k++;
                }
                if(k == 3)
                {
                    //Check to see if device exists
                    if(strcmp(argument[0],device[0])==0)
                    {
                        //Check to see if block size correct for system
                        if(atoi(argument[2]) != BLOCK_SIZE)
                        {
                            printf("ERROR: USE BLOCK SIZE 4\n");
                        }
                        else
                        {
                            //Copy file system name to system and format device
                            strcpy(filesystem[0],argument[1]);
                            //Format the file system, create file system header
                            format();
                            int i;
                            for(i=0; i<100; i++)
                            {
                                directories[i][0]='\0';
                            }
                            for(i=0; i<60; i++)
                            {
                                files[i][0]='\0';
                            }
                            printf("FILE SYSTEM FORMATTED\n");
                            initiate=2;
                        }
                    }
                    else
                    {
                        printf("ERROR: DEVICE NOT KNOWN\n");
                    }
                }
                else
                    printf("ERROR: INCORRECT ARGUMENTS\n");
            }
		}
        //If command is mount
		else if(strcmp(command, "mount") == 0 && initiate>0)
		{
            if(initiate>2)
                printf("ERROR: %s IS CURRENTLY MOUNTED TO THE SYSTEM\n",filesystem[0]);
            else
            {
                int k = 0;
                temp = strtok(NULL, " ");
                //Check to see if correct number of arguments
                while(temp != NULL)
                {
                    strcpy(argument[k], temp);
                    temp = strtok(NULL, " ");
                    k++;
                }
                //If not correct arguments
                if(k == 1)
                {
                    //if file system exists, mount it
                    if(strcmp(filesystem[0],argument[0]) == 0)
                    {
                        //Set mount bit in File System header
                        mountfs();
                        printf("%s MOUNTED TO SYSTEM\n",filesystem[0]);
                        initiate=3;
                    }
                    else
                    {
                        printf("ERROR: %s DOES NOT EXIST ON DEVICE %s\n",argument[0],device[0]);
                    }
                }
                else
                {
                    printf("ERROR: INCORRECT ARGUMENTS\n");
                }
            }
		}
        //If commnad is create_directory
		else if(strcmp(command, "create_directory") == 0 && initiate>0)
		{
            if(initiate<3)
                printf("ERROR: FILE SYSTEM ON KNOWN DEVICE MUST BE MOUNTED TO SYSTEM\n");
            else
            {
                temp = strtok(NULL," ");
                int k=0;
                //Check to see if correct number of arguments
                while(temp != NULL)
                {
                    strcpy(argument[k], temp);
                    temp = strtok(NULL, " ");
                    k++;
                }
                if(k== 1)
                {
                    //Check to see if directory name already exists
                    int x=0,check=0;
                    while(x<100 && check == 0)
                    {
                        if(strcmp(directories[x],argument[0])==0)
                            check=1;
                        else
                            x++;
                    }
                    if(check == 1)
                    {
                        printf("ERROR: DIRECTORY NAME IS NOT UNIQUE\n");
                    }
                    else
                    {
                        //If directory name unique, place it in directories listing in free spot
                        int i=0,temp1=-1;
                        while(i < 100 && temp1==-1)
                        {
                            if(directories[i][0]=='\0')
                                temp1=1;
                            else
                                i++;
                        }
                        if(temp1==1)
                        {
                            strcpy(directories[i],argument[0]);
                            //Create dictionary with ID i
                            create_directory(i);
                            dirCount++;
                        }
						else
							printf("ERROR: CAN'T CREATE ANY MORE DIRECTORIES\n");
                    }
                }
                else
                {
                    printf("ERROR: INCORRECT ARGUMENTS\n");
                }
            }
		}
        else if(strcmp(command, "open") == 0 && initiate>0)
        {
            if(initiate<3)
                printf("ERROR: FILE SYSTEM ON KNOWN DEVICE MUST BE MOUNTED TO SYSTEM\n");
            else
            {
                temp = strtok(NULL," ");
                int k=0;
                //Check to see if correct number of arguments
                while(temp != NULL)
                {
                    strcpy(argument[k], temp);
                    temp = strtok(NULL, " ");
                    k++;
                }
                if(k== 3)
                {
                    //Check to see if directory exists
                    int x=0;check=0;
                    while(x<100 && check == 0)
                    {
                        if(strcmp(directories[x],argument[0])==0)
                            check=1;
                        else
                            x++;
                    }
                    if(check != 1)
                    {
                        printf("ERROR: DIRECTORY NOT FOUND\n");
                    }
                    else
                    {
                        //Check to see if file exists already on system
                        int y=0,check1=0;
                        while(y<60 && check1 == 0)
                        {
                            if(strcmp(files[y],argument[1])==0)
                                check1=1;
                            else
                                y++;
                        }
                        
                        //Create new file of type new
                        if(strcmp(argument[2], "new") == 0)
                        {
                            //If the requested file name was unique
                            if(check1 == 0)
                            {
                                int i=0,temp1=-1;
                                while(i < 60 && temp1==-1)
                                {
                                    if(files[i][0]=='\0')
                                        temp1=1;
                                    else
                                        i++;
                                }
                                if(temp1==1)
                                {
                                    //Return the number of blocks for the file
                                    //Open file - Sending directory id, file id, new bit
                                    int temp2 = openFile(x,i,0);
                                    if(temp2 > 0)
                                    {
										strcpy(files[i],argument[1]);
                                        printf("FILE %s ASSIGNED WITH %d BLOCKS\n",files[i],temp2);
                                        fileCount++;
                                    }
                                    else
                                        printf("ERROR: FILE NOT CREATED\n");
                                }
                                else
                                    printf("ERROR: MAX NUMBER OF FILES\n");
                            }
                            //If the requested file name was not unique, error
                            else
                                printf("ERROR: FILE NAME NOT UNIQUE\n");
                        }
                        //If file exists, set to read only
                        else if (strcmp(argument[2], "read-only") == 0 && check1==1)
                        {
                            //Sending directory id, file id, read bit
                            //File System Manager will determine if file is under directory
                            int temp2 = openFile(x,y,1);
                            if(temp2 == -1)
                                printf("ERROR: FILE NOT IN SPECIFIED DIRECTORY\n");
                            else
                                printf("%s/%s NOW HAS PERMISSION: READ-ONLY\n",directories[x],files[i]);
                            
                        }
                        //If file exists, set to read-write
                        else if (strcmp(argument[2], "read-write") == 0 && check1==1)
                        {
                            //Sending directory id, file id, read-write bit
                            //File System will determine if file is under directory
                            int temp2 = openFile(x,y,2);
                            if(temp2 == -1)
                                printf("ERROR: FILE NOT IN SPECIFIED DIRECTORY\n");
                            else
                                printf("%s/%s NOW HAS PERMISSION: READ-WRITE\n",directories[x],files[i]);
                        }
                        //If file does not exist and trying to change permission, error
                        else if(check1 == 0)
                            printf("ERROR: FILE NAME NOT RECOGNIZED\n");
                        //If permission argument not recognized, error
                        else
                            printf("ERROR: FILE TYPE NOT RECOGNIZED\n");
                    }
                }
                else
                {
                    printf("ERROR: INCORRECT ARGUMENTS\n");
                }
            }
        }
        else if(strcmp(command, "read") == 0 && initiate>0)
        {
            if(initiate<3)
                printf("ERROR: FILE SYSTEM ON KNOWN DEVICE MUST BE MOUNTED TO SYSTEM\n");
            else
            {
                temp = strtok(NULL," ");
                int k=0;
                //Check to see if correct number of arguments
                while(temp != NULL)
                {
                    strcpy(argument[k], temp);
                    temp = strtok(NULL, " ");
                    k++;
                }
                if(k== 4)
                {
                    //Check to see if directory exists on system
                    int x=0;check=0;
                    while(x<100 && check == 0)
                    {
                        if(strcmp(directories[x],argument[0])==0)
                            check=1;
                        else
                            x++;
                    }
                    if(check == 0)
                    {
                        printf("ERROR: DIRECTORY NOT FOUND\n");
                    }
                    else
                    {
                        //check to see if file hande exists
                        if(files[atoi(argument[1])][0] == '\0')
                        {
                            printf("ERROR: INVALID FILEHANDLE\n");
                        }
                        else
                        {
                            //If file exists, check to see if block size is in range of file size
                            if(atoi(argument[2]) < MAX_FILE_SIZE && atoi(argument[2]) >= 0)
                            {
                                //If in range, check to see if buffer exists
                                if(atoi(argument[3]) < MAX_BUF && atoi(argument[3]) >= 0)
                                {
                                    //Send command
                                    //Sending directory id, file id, block number and buffer number
                                    //File system will determine in file in directory
                                    int getReturn = readBlock(x,atoi(argument[1]),atoi(argument[2]),atoi(argument[3]));
                                    if(getReturn == -1)
                                        printf("ERROR: NO DATA IN BLOCK FOR FILE\n");
                                    else if(getReturn == -2)
                                        printf("ERROR: FILE NOT OPEN TO READ\n");
                                    else if(getReturn == -3)
                                        printf("ERROR: FILE DOES NOT EXIST IN DIRECTORY\n");
                                    else if(getReturn == -4)
                                        printf("ERROR: BLOCK NOT WRITTEN TO YET FOR FILE\n");
                                    else if(getReturn == -5)
										printf("DATA HAS BEEN CORRUPTED FILE DELETED");
									else if(getReturn == -6)
										printf("Problem");
                                    else
                                        printf("BLOCK %d READ FROM FILE %s\n",atoi(argument[2]),files[atoi(argument[1])]);
                                }
                                else
                                {
                                    printf("ERROR: BUFFER NOT FOUND\n");
                                }
                            }
                            else
                            {
                                printf("ERROR: BLOCK SIZE NOT IN LIMIT OF FILE\n");
                            }
                        }
                    }
                }
                else
                {
                    printf("ERROR: INCORRECT ARGUMENTS\n");
                }
            }
        }
        else if(strcmp(command, "write") == 0 && initiate>0)
        {
            if(initiate<3)
                printf("ERROR: FILE SYSTEM ON KNOWN DEVICE MUST BE MOUNTED TO SYSTEM\n");
            else
            {
                temp = strtok(NULL," ");
                int k=0;
                //Check to see if correct number of arguments
                while(temp != NULL)
                {
                    strcpy(argument[k], temp);
                    temp = strtok(NULL, " ");
                    k++;
                }
                if(k== 4)
                {
                    //Check to see if directory exists
                    int x=0;check=0;
                    while(x<100 && check == 0)
                    {
                        if(strcmp(directories[x],argument[0])==0)
                            check=1;
                        else
                            x++;
                    }
                    if(check == 0)
                    {
                        printf("ERROR: DIRECTORY NOT FOUND\n");
                    }
                    else
                    {
                        //Check to see if file handle exists
                        if(files[atoi(argument[1])][0] == '\0')
                        {
                            printf("ERROR: INVALID FILEHANDLE\n");
                        }
                        else
                        {
                            //Check to see if block size in limit of file size
                            if(atoi(argument[2]) < MAX_FILE_SIZE && atoi(argument[2]) >= 0)
                            {
                                //Check to see if buffer is in limit of buffers
                                if(atoi(argument[3]) < MAX_BUF && atoi(argument[3]) >= 0)
                                {
                                    //Send command to write block to disk
                                    //Sending directory id, file id, block number and buffer number
                                    //File system will determine in file in directory
                                    int getReturn = writeBlock(x,atoi(argument[1]),atoi(argument[2]),atoi(argument[3]));
                                    if(getReturn == -1)
                                        printf("ERROR: FILE NOT OPEN TO WRITE\n");
                                    else if(getReturn == -3)
                                        printf("ERROR: FILE NOT IN SPECIFIED DIRECTORY\n");
                                    else if(getReturn == -4)
                                        printf("BLOCK %d WRITTEN TO FILE %s AGAIN\n",atoi(argument[2]),files[atoi(argument[1])]);
                                    else
                                        printf("BLOCK %d WRITTEN TO FILE %s\n",atoi(argument[2]),files[atoi(argument[1])]);
                                }
                                else
                                {
                                    printf("ERROR: BUFFER NOT FOUND\n");
                                }
                            }
                            else
                            {
                                printf("ERROR: BLOCK SIZE NOT IN LIMIT OF FILE\n");
                            }
                        }
                    }
                }
                else
                {
                    printf("ERROR: INCORRECT ARGUMENTS\n");
                }
            }
        }
        else if(strcmp(command, "close") == 0 && initiate>0)
        {
            if(initiate<3)
                printf("ERROR: FILE SYSTEM ON KNOWN DEVICE MUST BE MOUNTED TO SYSTEM\n");
            else
            {
                temp = strtok(NULL," ");
                int k=0;
                //Check to see if correct number of arguments
                while(temp != NULL)
                {
                    strcpy(argument[k], temp);
                    temp = strtok(NULL, " ");
                    k++;
                }
                if(k== 2)
                {
                    //Check to see if directory exists
                    int x=0;check=0;
                    while(x<100 && check == 0)
                    {
                        if(strcmp(directories[x],argument[0])==0)
                            check=1;
                        else
                            x++;
                    }
                    if(check == 0)
                    {
                        printf("\nERROR: DIRECTORY NOT FOUND\n");
                    }
                    else
                    {
                        //Check to see if file handle exists
                        if(files[atoi(argument[1])][0] == '\0')
                        {
                            printf("ERROR: INVALID FILEHANDLE\n");
                        }
                        else
                        {
                            //Send command to close file
                            //Sending Directory id and file id
                            int getReturn = closeFile(x,atoi(argument[1]));
                            if(getReturn == -1)
                                printf("ERROR: FILE NOT IN GIVEN DIRECTORY\n");
                            else if(getReturn == -2)
                                printf("ERROR: FILE NOT OPENED\n");
                            else
                                printf("FILE CLOSED\n");
                        }
                    }
                }
                else
                {
                    printf("ERROR: INCORRECT ARGUMENTS\n");
                }
            }
        }
        else if(strcmp(command, "delete") == 0 && initiate>0)
        {
            if(initiate<3)
                printf("ERROR: FILE SYSTEM ON KNOWN DEVICE MUST BE MOUNTED TO SYSTEM\n");
            else
            {
                temp = strtok(NULL," ");
                int k=0;
                //Check to see if correct number of arguments
                while(temp != NULL)
                {
                    strcpy(argument[k], temp);
                    temp = strtok(NULL, " ");
                    k++;
                }
                if(k == 1 || k == 2)
                {
                    //Check to see if directory exists
                    int x=0;check=0;
                    while(x<100 && check == 0)
                    {
                        if(strcmp(directories[x],argument[0])==0)
                            check=1;
                        else
                            x++;
                    }
                    if(check == 0)
                    {
                        printf("ERROR: DIRECTORY NOT FOUND\n");
                    }
                    else
                    {
                        //If deleting file, check to see if file exists
                        if(k == 2)
                        {
                            int y=0,check1=0;
                            while(y<60 && check1 == 0)
                            {
                                if(strcmp(files[y],argument[1])==0)
                                    check1=1;
                                else
                                    y++;
                            }
                            if(check1 == 0)
                            {
                                printf("ERROR: FILE NAME DOES NOT EXIST\n");
                            }
                            else
                            {
                                //Deletes the file y under directory x
                                //File System manager will determine if file under directory
                                int* getReturn = deleteObjects(x,y);  
                                if(getReturn[0] == -1)
                                    printf("ERROR: FILE DOES NOT EXIST IN DIRECTORY\n");
                                else if(getReturn[0]== -2)
                                    printf("ERROR: %s OPEN IN DIRECTORY. COULD NOT EXECUTE DELETE\n",files[getReturn[1]]);
								else
                                {
                                    printf("FILE DELETED: %s\n",files[y]);
									files[y][0]='\0';
                                }
                                
                            }
                        }
                        else
                        {
                            //-1 indicates that we are deleting the directory with ID x
                            int* getReturn = deleteObjects(x,-1);  
                            if(getReturn[0]==-1)
                                printf("ERROR: FILE DOES NOT EXIST IN DIRECTORY\n");
                            else if(getReturn[0]== -2)
                                printf("ERROR: %s OPEN IN DIRECTORY. COULD NOT EXECUTE DELETE\n",files[getReturn[1]]);
							else
							{
                                
                                printf("FILES DELETED WITH DIRECTORY:\n");
								int z=0;
								while(getReturn[z] != -6)
								{
                                    printf("%s\n",files[getReturn[z]]);
									files[getReturn[z]][0]= '\0';
									z++;
								}
                                
                                printf("DIRECTORY %s DELETED\n",directories[x]);
                                directories[x][0]='\0';
								
							}
                        }
                    }
                }
                else
                {
                    printf("ERROR: INCORRECT ARGUMENTS\n");
                }
            }
        }
        /*else if(strcmp(command, "bad_block") == 0 && initiate>0)
        {
            if(initiate<3)
                printf("ERROR: FILE SYSTEM ON KNOWN DEVICE MUST BE MOUNTED TO SYSTEM\n");
            else
            {
                temp = strtok(NULL," ");
                int k=0;
                //Check to see if correct number of arguments
                while(temp != NULL)
                {
                    strcpy(argument[k], temp);
                    temp = strtok(NULL, " ");
                    k++;
                }
                if(k== 1)
                {
                    //Check to see if block number exists on Hard Disk
                    if(atoi(argument[0]) < MAX_HD_SIZE && atoi(argument[0]) >= 0)
                    {
                        //Sending block number on hard drive to file system manager
                        int getReturn= bad_block(atoi(argument[0]));
                        if(getReturn == -1)
                            printf("ERROR: BLOCK ALREADY BAD\n");
                        //If block containing device header goes bad, need to init new device
                        else if(getReturn == -2)
                        {
                            printf("ERROR: DEVICE HEADER/HARD DRIVE CRASH. INITIALIZE NEW DEVICE\n");
                            initiate=0;
                        }
                        //If block containing file system header goes bad, need to reformat.
                        else if(getReturn == -3)
                        {
                            printf("ERROR: FILE SYSTEM LOST - REFORMAT DRIVE\n");
                            initHD();
                            initiate=1;
                        }
                    }
                    else
                    {
                        printf("ERROR: BLOCK SIZE NOT IN LIMIT OF HARD DISK\n");
                    }
                    
                }
                else
                {
                    printf("ERROR: INCORRECT ARGUMENTS\n");
                }
            }
        }
		*/
		else if(strcmp(command, "unmount") == 0 && initiate>0)
        {
            if(initiate<3)
                printf("ERROR: FILE SYSTEM ON KNOWN DEVICE MUST BE MOUNTED TO SYSTEM\n");
            else
            {
                temp = strtok(NULL," ");
                int k=0;
                //Check to see if correct number of arguments
                while(temp != NULL)
                {
                    strcpy(argument[k], temp);
                    temp = strtok(NULL, " ");
                    k++;
                }
                if(k== 1)
                {
                    //Check to see if file system exists, unmount it if it does
                    if(strcmp(filesystem[0],argument[0]) == 0)
                    {
                        //Set mount bit in file system header to unmount
                        int getReturn = unmountfs();
                        if(getReturn == -1)
                            printf("ERROR: FILE SYSTEM ALREADY UNMOUNTED\n");
                        else
                        {
                            printf("%s UNMOUNTED FROM SYSTEM\n",filesystem[0]);
                            initiate=2;
                        }
                    }
                    else
                    {
                        printf("ERROR: %s DOES NOT EXIST ON DEVICE %s\n",argument[0],device[0]);
                    }
                }
                else
                {
                    printf("ERROR: INCORRECT ARGUMENTS\n");
                }
            }
        }
		else if(strcmp(command, "EXIT") == 0 || strcmp(command, "exit") == 0)
		{
            exit = 0;
		}
        //If init_IPC has not been called initially, error
        else if(initiate==0)
        {
            printf("ERROR: PLEASE INITIATE DEVICE\n");
        }
        //If command not recognized, error
		else
		{
			printf("ERROR: COMMAND NOT RECOGNIZED\n");
		}
	}
	return 0;
}


//Lists the messages in a queue
void list(int select, int id)
{
    //Get pointer to Hard Disk
    file_control_block *hd = getHardDrive();
    
    //Get List of Devices
    if(select == -2 || select == -1)
    {
        printf("DEVICES\n");
        printf("%s - ",device[0]);
        if(strcmp(filesystem[0],"\0") != 0)
        {
            printf("FILE SYSTEM: %s - ",filesystem[0]);
            if(hd[266].mounted == 0)
                printf("NOT MOUNTED\n");
            else if(hd[266].mounted == 1)
                printf("MOUNTED\n");
			printf("\n");
        }
        else
            printf("NO FILE SYSTEM ON DEVICE\n");
    }
    //List file info
    if((select == -3 || select == -1) && hd[266].mounted != 0)
    {
        //Prints the information about the specified file
        if(id != -1)
        {
            int numDir = hd[267].ptrSize;
            int i=0,j=0,temp1=0;
            //Look for all directories
            while(i<numDir && temp1==0)
            {
                j=0;
                int dirLoc = hd[267].pointers[i].location;
                //Search through all of directory for file
                while(j<hd[dirLoc].ptrSize && temp1==0)
                {
                    //If file found, print out information
                    if(hd[dirLoc].pointers[j].name == id)
                    {
                        int fileLoc = hd[dirLoc].pointers[j].location;
                        printf("FILEINFO: %s\n",files[id]);
						printf("FILEHANDLE: %i\n",id);
                        printf("Parent Directory: %s\n",directories[hd[dirLoc].ID]);
                        printf("File Header Location: %d\n",fileLoc);
                        printf("Size of File: %dKB\n",hd[fileLoc].ptrSize*4);
                        printf("Number of Blocks: %d\n",hd[fileLoc].ptrSize);
                        printf("Block Size: 4KB\n");
                        int z;
                        for(z=0; z<hd[fileLoc].ptrSize; z++)
                        {
                            printf("BLOCK: %d LOCATION: %d\n",hd[fileLoc].pointers[z].name,hd[fileLoc].pointers[z].location);
                        }
                        if(hd[fileLoc].permission == 0)
                            printf("Permission: New\n");
                        else if(hd[fileLoc].permission == 1)
                            printf("Permission: Read-Only\n");
                        else if(hd[fileLoc].permission == 2)
                            printf("Permission: Read-Write\n");
                        
                        if(hd[dirLoc].pointers[j].valid == 0)
                            printf("File Opened\n");
                        else if(hd[dirLoc].pointers[j].valid == 1)
                            printf("File Closed\n");
                        temp1=1;
						printf("\n");
                    }
                    else
                        j++;
                }
                i++;
            }
        }
        //Prints information about all files
        else
        {
            int numDir = hd[267].ptrSize;
            int k=0,j=0;
            //For every directory, print out every file in directory
            while(k<numDir)
            {
                //print of information for file in directory
                j=0;
                int dirLoc = hd[267].pointers[k].location;
                while(j<hd[dirLoc].ptrSize)
                {
                    int fileLoc = hd[dirLoc].pointers[j].location;
                    printf("FILEINFO: %s\n",files[hd[fileLoc].ID]);
					printf("FILEHANDLE: %i\n",hd[fileLoc].ID);
                    printf("Parent Directory: %s\n",directories[hd[dirLoc].ID]);
                    printf("File Header Location: %d\n",fileLoc);
                    printf("Size of File: %dKB\n",hd[fileLoc].ptrSize*4);
                    printf("Number of Blocks: %d\n",hd[fileLoc].ptrSize);
                    int z;
                    for(z=0; z<hd[fileLoc].ptrSize; z++)
                    {
                        printf("BLOCK: %d LOCATION: %d\n",hd[fileLoc].pointers[z].name,hd[fileLoc].pointers[z].location);
                    }
                    printf("Block Size: 4KB\n");
                    
                    if(hd[fileLoc].permission == 0)
                        printf("Permission: New\n");
                    else if(hd[fileLoc].permission == 1)
                        printf("Permission: Read-Only\n");
                    else if(hd[fileLoc].permission == 2)
                        printf("Permission: Read-Write\n");
                    
                    if(hd[dirLoc].pointers[j].valid == 0)
                        printf("File Opened\n");
                    else if(hd[dirLoc].pointers[j].valid == 1)
                        printf("File Closed\n");
                    printf("\n");
                    j++;
                }
                k++;
            }
            
        }
    }
    //Print Directories with file names
    if((select == -4 || select == -1) && hd[266].mounted != 0)
    {
        //Prints the specified directory and its files
        if(id>=0)
        {
            
            int numDir = hd[267].ptrSize;
            int i=0,j=0,temp1=0;
            while(i<numDir && temp1==0)
            {
                //If directory found, print out all files in directory
                if(hd[267].pointers[i].name == id)
                {
                    j=0;
                    int dirLoc = hd[267].pointers[i].location;
                    printf("DIRECTORY: %s - Location: %d\n",directories[id],dirLoc);
					printf("DIRECTORY ID: %i\n", id);
                    
                    while(j<hd[dirLoc].ptrSize && temp1==0)
                    {
                        printf("%s\n",files[hd[dirLoc].pointers[j].name]);
                        j++;
                    }
					printf("\n");
                    temp1=1;
                }
                else
                    i++;
            }
        }
        //Prints all Directories and the files contained in them
        else
        {
            
            int numDir = hd[267].ptrSize;
            int k=0,j=0;
            while(k<numDir)
            {
                j=0;
                int dirLoc = hd[267].pointers[k].location;
                printf("DIRECTORY: %s - Location: %d\n",directories[hd[dirLoc].ID],dirLoc);
				printf("DIRECTORY ID: %i\n", hd[dirLoc].ID);
                while(j<hd[dirLoc].ptrSize)
                {
                    printf("%s\n",files[hd[dirLoc].pointers[j].name]);
                    j++;
                }
				printf("\n");
                k++;
            }
        }
        
    }
}
